<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-10-03 14:20
 *
 * 项目：rm  -  $  - levmodulesSetHelper.php
 *
 * 作者：liwei 
 */


namespace modules\levmodules\helpers;

!defined('INLEV') && exit('Access Denied LEV');

use Lev;
use lev\base\Migrationv;
use lev\helpers\dbHelper;
use lev\helpers\ModulesHelper;
use lev\helpers\SettingsHelper;
use lev\helpers\UrlHelper;

class levmodulesSetHelper
{

    public static function zipMdir()
    {
        return rtrim(trim(Lev::stget('zipMdir', 'levmodules')), '/');
    }

    public static function LevVersion() {
        return trim(Lev::stget('LevVersion', 'levmodules'));
    }

    public static function storeApiDomain() {
        return rtrim(trim(Lev::stget('storeApiDomain', 'levmodules')) ?: UrlHelper::store(false), '/');
    }

    public static function devloperUid() {
        return Lev::stget('devloperUid', 'levmodules');
    }
    public static function accesstoken() {
        return Lev::stget('accesstoken', 'levmodules') ?: zipUploadServerHelper::getStoreAccesstoken();
    }


    public static function setformInputs() {
        $arr['<tips>提示：选择后将自动创建一个表单模型文件，前提是所选项内有设置字段</tips>'][0] = '无';

        $iden = empty(Lev::$app['inputOptionInit']['#iden']) ? Lev::$app['iden'] : Lev::$app['inputOptionInit']['#iden'];
        $setClassify = ModulesHelper::getClassify($iden);
        $arr['我的自定义设置分类字段'] = ModulesHelper::getSetClassify($iden, $setClassify, true);
        $arr['设置分类字段'] = $setClassify;

        $arr['数据表字段<tips>创建控制器的同时已自动创建；如果设置分类下存在字段</tips>'] = SettingsHelper::tabClassify($iden);

        return $arr;
    }

    /**
     * 获得模块卸载数据表
     * @return array
     */
    public static function settableName() {
        $arr['<tips>提示：选择后将自动创建控制器文件，否则只是单纯的添加导航。已创建的不再重复创建，如需重建请先删除</tips>'][0] = '无';
        $groupkey = '模块表';
        $arr[$groupkey] = [];

        $iden = empty(Lev::$app['inputOptionInit']['#iden']) ? Lev::$app['iden'] : Lev::$app['inputOptionInit']['#iden'];

        $mudInfo = ModulesHelper::getModuleInfo($iden);
        $tables = Lev::arrv('dropTables', Lev::getSettings($mudInfo['settings']), []);
        if ($tables) {
            foreach ($tables as $table) {
                $tableName = dbHelper::tableName($table);
                $arr[$groupkey][$table] = $tableName;
            }
        }
        if ($iden === 'levmodules') {
            $arr += static::setbaseTableName();
        }
        return $arr;
    }
    public static function setbaseTableName() {
        $arr = [];
        $tables = Migrationv::getLevBaseTables();
        foreach ($tables as $table) {
            $tableName = dbHelper::tableName($table);
            $arr[$table] = $tableName;
        }
        return ['基本表'=>$arr];
    }

    public static function openGit() {
        return !Lev::stget('openGit', 'levmodules');
    }

    public static function serversOpen() {
        if (!static::openGit()) return [];

        $arr = [];
        $servers = Lev::stget('servers', 'levmodules');
        if ($servers) {
            foreach ($servers as $v) {
                if (empty($v['status']) && $v['serverDomain']) {
                    $arr[$v['id']] = $v;
                }
            }
        }
        return $arr;
    }
}